package GUI;




import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;


import java.awt.Rectangle;
import java.io.IOException;

import javax.swing.JLabel;

import Logic.ServerHandle;


import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import javax.swing.JList;


/**
 * This class responsible Server's GUI
 * @author Evgeny
 * @author Igor
 * @author Samir
 * @author Konstantin
 */

public class ServerMainGUI extends JFrame {


	private static final long serialVersionUID = 1L;

	private JPanel FirstPanel = null;
	private ServerHandle server=null;  

	private JButton closeBtn = null;

	private JList clientConnectionList = null;
	
	private DefaultListModel clientConnectionModelList=null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;
	
	


	/**
	 * This is the default constructor
	 */
	public ServerMainGUI() {
		super();
		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 400);
		this.setContentPane(getFirstPanel());
		this.setTitle("Roberg Server Side");
		server=new ServerHandle(5555);
		 try 
		    {
			 server.listen(); //Start listening for connections
		    } 
		    catch (Exception ex) 
		    {
		      System.out.println("ERROR - Could not listen for clients!");
		    }
		
		
	}
	/**
	 * gets the main panel
	 * 
	 * @return JPanel with the elements of the screen
	 */
	public JPanel getFirstPanel() {
		if (FirstPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(230, 73, 335, 25));
			jLabel1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));
			jLabel1.setText("Connected Remote mashine:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(230, 21, 481, 41));
			jLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
			jLabel.setText("Roberg Restaurant Server");
			FirstPanel = new JPanel();
			FirstPanel.setLayout(null);
			FirstPanel.add(getCloseBtn(), null);
			FirstPanel.add(getClientConnectionList(), null);
			FirstPanel.add(jLabel, null);
			FirstPanel.add(jLabel1, null);
			
			
		}
		return FirstPanel; 
	}
	


	/**
	 * This method initializes closeBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCloseBtn() {
		if (closeBtn == null) {
			closeBtn = new JButton();
			closeBtn.setText("Close Connection");
			closeBtn.setSize(new Dimension(200, 30));
			closeBtn.setLocation(new Point(331, 329));
			closeBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					server.stopListening();
					try {
						server.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					JOptionPane.showMessageDialog(null,"Server is shutting down");
					System.exit(ABORT);
				
				}
			});
		}
		return closeBtn;
	}

	/**
	 * This method initializes ClientConnectionList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getClientConnectionList() {
		if (clientConnectionList == null) {
			clientConnectionList = new JList();
			clientConnectionList.setSize(new Dimension(415, 203));
			clientConnectionList.setLocation(new Point(230, 109));
		}
		return clientConnectionList;
	}
	
	/**
	 * gets the clients connected to the server into a list model
	 * 
	 * @return DefaultListModel of clients connected to the server
	 */
	private DefaultListModel getConnectedClients()
	{
		clientConnectionModelList=new DefaultListModel();
		if(server.getNumberOfClients()==0)
		{
			clientConnectionModelList.addElement("No Connections at the moments");
		}
		else
		{
		Thread[] thrArr=null;
		clientConnectionModelList=new DefaultListModel();
		thrArr=server.getClientConnections();
		for(int i=0;i<server.getNumberOfClients();i++)
			clientConnectionModelList.addElement(thrArr[i]);
		}
			
		
		return clientConnectionModelList;
	}
	/**
	 * sets the clients connected to the server from the DefaultListModel into the clientConnectionList
	 */
	public void usersConnected() {
		   clientConnectionList.setModel(getConnectedClients());
		  
		 }
	


}  //  @jve:decl-index=0:visual-constraint="10,10"
